package com.hy.study.practice.queue;

import java.util.Scanner;

/**
 * 单项队列的练习
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SingleArrayQueue queue = new SingleArrayQueue(4);
        try {
            char type=' ';
            for(;;){
                System.out.println("--------请选择----------");
                System.out.printf("a(add) e(exit) g(get) s(show) h(head) \n");
                type=scanner.next().charAt(0);
                switch (type){
                    case 'a':
                        System.out.println("请输入一个数:");
                        try {
                            int value = scanner.nextInt();
                            queue.add(value);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'g':
                        try {
                            System.out.println(queue.get());
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 's':
                        try {
                            queue.show();
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'h':
                        try {
                            System.out.println(queue.head());
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'e':
                        System.out.println("程序退出");
                        scanner.close();
                        System.exit(0);
                    default:
                        break;
                }
            }

        }finally {
            scanner.close();
        }
    }

}

class SingleArrayQueue {
    private int rear;
    private int maxSize = 16;
    private int arr[];
    private int front;

    public SingleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.rear = -1;
        this.front = -1;
        this.arr = new int[maxSize];
    }

    public SingleArrayQueue() {
        this.rear = -1;
        this.front = -1;
        this.arr = new int[maxSize];
    }

    /**
     * 判断是否为满
     *
     * @return
     */
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 添加元素
     *
     * @param value
     */
    public void add(int value) {
        if(isFull()){
            throw new RuntimeException("队列已满,无法添加");
        }
        rear++;
        arr[rear]=value;
    }

    /**
     * 获取队列元素
     * @return
     */
    public int get(){
        if(isEmpty()){
            throw  new RuntimeException("队列为空,无法获取");
        }
        return arr[++front];
    }

    /**
     * 获取队列头元素
     * @return
     */
    public int head(){
        if(isEmpty()){
            throw  new RuntimeException("队列为空,无法获取");
        }
        return arr[front+1];
    }

    /**
     * 展示队列中所有的元素
     */
    public void show(){
        if(isEmpty()){
            throw  new RuntimeException("队列为空,无法获取");
        }
        for(int i=front+1;i<rear;i++){
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
}
